package androidx.gdata.spreadsheets.xml;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.util.LinkedList;
import java.util.List;

import android.util.Log;
import androidx.gdata.LinkUrl;
import androidx.gdata.spreadsheets.GSX;
import androidx.gdata.spreadsheets.ListEntry;
import androidx.gdata.xml.ThingWithLinksDeserializer;

public class ListEntryDeserializer extends
  ThingWithLinksDeserializer<ListEntry> {
  
 
  private static GSXDeserializer gsxBuilder = new GSXDeserializer();

  @Override
  public ListEntry build(Node entryNode) {
    node = entryNode;
    Log.i("Notes", "Node = " + node.getNodeValue());
    String id = getText("id");
    String updated = getText("updated");
    String title = getText("title");
    LinkUrl[] links = getLinks();
    List<Node> gsxNodes = getGSXNodes();
    GSX[] gsx = new GSX[gsxNodes.size()];
    int i = 0;
    for (Node gsxNode : gsxNodes) {
      gsx[i++] = gsxBuilder.build(gsxNode);
    }
    return new ListEntry(id, updated, title, links, gsx);
  }
  
  public List<Node> getGSXNodes() {
    LinkedList<Node> nodes = new LinkedList<Node>();
    NodeList nodeList = node.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
      if (nodeList.item(i).getNodeName().startsWith("gsx")) {
        nodes.add(nodeList.item(i));
      }
    }
    return nodes;
  }

}
